<?php

//declare (strict_types=1);

namespace app\admin\middleware;

class Auth {

    protected $whileController = [
        '/login/',
        '/test/',
        '/captcha/',
    ];

    public function handle($request, \Closure $next) {

        // 前置中间件
        if (empty(session(config("admin.session_admin"))) && !$this->checkPathInfo($request->pathinfo())) {
            //return redirect(url('login/index'));
        }

        $response = $next($request);
        return $response;
    }

    /**
     * 中间件结束调度
     * @param \think\Response $response
     */
    public function end(\think\Response $response) {

    }

    private function checkPathInfo($path) {
        foreach ($this->whileController as $item) {
            if (preg_match($item, $path)) {
                return true;
            };
        }
        return false;
    }
}